<template>
  <div>
    御剑乘风来,除魔天地间!==分配权限=={{ defaultKeys }}
    <el-tree
      :data="list"
      show-checkbox
      node-key="id"
      ref="tree"
      default-expand-all
      :check-strictly="true"
      :default-checked-keys="defaultKeys"
      :props="defaultProps"
    >
    </el-tree>

    <div style="margin-top: 20px; text-align: right">
      <el-button type="primary" @click="onSubmit">确定</el-button>
      <el-button @click="closeDialog">取消</el-button>
    </div>
  </div>
</template>
<script>
import arrayToTree from 'array-to-tree'
import { getPermissionList } from '@/api/permisson'
import { getRoleDetail, assignPerm } from '@/api/settings'
export default {
  name: '',
  props: ['roleId'],
  data() {
    return {
      list: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      defaultKeys: [],
      permIds: []
    }
  },
  created() {
    this.getPermission()
    //  获取当前角色所拥有的权限
    this.getRight()
  },
  computed: {},
  methods: {
    async getPermission() {
      const res = await getPermissionList()
      console.log(res, 71)
      this.list = arrayToTree(res.data, {
        parentProperty: 'pid',
        customID: 'id'
      })
    },
    closeDialog() {
      this.$emit('close')
      this.defaultKeys = []
    },
    onSubmit() {
      console.log(this.$refs.tree.getCheckedKeys(), 2233)
      this.permIds = this.$refs.tree.getCheckedKeys() //  获取当前选中的权限id集合 []

      this.doSubmit()
      this.closeDialog() //  关闭对话框
    },
    async doSubmit() {
      const res = await assignPerm({
        id: this.roleId,
        permIds: this.permIds
      })
      console.log(res, 33344)
    },
    /* 根据角色id获取权限 */
    async getRight() {
      const res = await getRoleDetail(this.roleId)
      console.log(res, 4)
      this.defaultKeys = res.data.permIds
    }
  }
}
</script>
<style lang='less'  scoped>
</style>
